package day20171129;

import com.mysql.cj.core.exceptions.CJOperationNotSupportedException;

import java.sql.*;

/**
 * Created by Administrator on 2017-11-29.
 */
public class Test {

    public static void main(String[] args) {


        // 3 + 1
        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;


        String sql = "select * from t_users";


        try {
            // 加载驱动程序
            Class.forName("com.mysql.jdbc.Driver");

            /**
             *
             * 数据库连接字符串
             * 数据库用户名
             * 数据库密码
             */
            connection = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/test",
                    "root",
                    "123456"
            );

            statement = connection.createStatement();

            // 对于增删改来说比较简单，只有一行代码
            statement.execute(sql);


            // 只有“查”
            resultSet = statement.executeQuery(sql);


            while (resultSet.next()) {

                resultSet.getInt(1);
                resultSet.getString(2);


            }


        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {


            try {
                statement.close();
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }


    }


}
